package collections;
import java.util.Map;
import com.google.common.base.Objects;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
/**
 * Table是由row和column两个因素决定的一个个cell组成的容器
 * 比Map<Double, Map<Double, String>>要优雅得多
 * @author tiangh
 * 2013-5-26 下午9:19:34
 */
public class TableDemo {

	private static Address[] addresses;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		initData();
		Table<Double, Double, String> addressTable = rowTable();
		System.out.println(addressTable.get(114.06667, 22.61667));
		
		
		Map<Double, Map<Double, String>> rowMap = addressTable.rowMap();
		Map<Double, Map<Double, String>> columnMap = addressTable.columnMap();
		System.out.println(rowMap);
		System.out.println(columnMap);
	}
	
	private static void initData() {
		Address a1=new Address(116.41667, 39.91667, "北京");
		Address a2=new Address(121.43333, 34.50000, "上海");
		Address a3=new Address(117.20000, 39.13333, "天津");
		Address a4=new Address(114.10000, 22.20000, "香港");
		Address a5=new Address(113.23333, 23.16667, "广州");
		Address a6=new Address(113.51667, 22.30000, "珠海");
		Address a7=new Address(114.06667, 22.61667, "深圳");
		Address a8=new Address(120.20000, 30.26667, "杭州");
		Address a9=new Address(106.45000, 29.56667, "重庆");
		Address a10=new Address(120.33333, 36.06667, "青岛");
		Address a11=new Address(118.10000, 24.46667, "厦门");
		Address a12=new Address(119.30000, 26.08333, "福州");
		addresses=new Address[]{a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12};
	}
	
	public static Table<Double, Double, String> rowTable() {
		Table<Double, Double, String> contactTable=HashBasedTable.create();
		for (Address address : addresses) {
			contactTable.put(address.longitude, address.latitude, address.name);
		}
		return contactTable;
	}
	
	public static class Address{
		double longitude;
		double latitude;
		String name;
		
		
		public Address() {
		}

		public Address(double longitude, double latitude, String name) {
			this.longitude = longitude;
			this.latitude = latitude;
			this.name = name;
		}



		@Override
		public int hashCode() {
			return Objects.hashCode(longitude,latitude,name);
		}
		
		@Override
		public boolean equals(Object obj) {
			return Objects.equal(this, obj);
		}
		
		@Override
		public String toString() {
			return Objects.toStringHelper(this)
					.add("longitude", longitude)
					.add("latitude", latitude)
					.add("name", name)
					.toString();
		}
	}
}
